package com.ibiaoke.system.service;

import com.google.common.collect.Lists;
import com.ibiaoke.common.base.Page;
import com.ibiaoke.common.utils.AssertUtils;
import com.ibiaoke.goods.dao.GoodsDao;
import com.ibiaoke.goods.dao.XcxGoodsDao;
import com.ibiaoke.goods.dao.XcxShopPriceDao;
import com.ibiaoke.goods.entity.Goods;
import com.ibiaoke.goods.entity.XcxGoods;
import com.ibiaoke.goods.entity.XcxShopPrice;
import com.ibiaoke.system.constant.FloorItemConst;
import com.ibiaoke.system.dao.FloorDao;
import com.ibiaoke.system.dao.FloorItemDao;
import com.ibiaoke.common.exception.ServiceException;
import com.ibiaoke.system.entity.Floor;
import com.ibiaoke.system.entity.FloorItem;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * Created by Administrator on 2017/6/16.
 */
@Service("floorService")
public class FloorServiceImpl implements FloorService {

    @Autowired
    private FloorDao floorDao;
    @Autowired
    private FloorItemDao floorItemDao;
    @Autowired
    private XcxGoodsDao xcxGoodsDao;
    @Autowired
    private XcxShopPriceDao xcxShopPriceDao;


    /**
     * 添加Floor
     *
     * @param floor
     * @return
     */
    @Override
    public int saveFloor(Floor floor) {
        if (StringUtils.isBlank(floor.getFloorName())) {
            throw new ServiceException("楼层名称不能为空");
        }
        floor.setDelStatus(0);
        floor.setCreateTime(new Date());
        floor.setUpdateTime(new Date());
        return floorDao.insertSelective(floor);
    }

    /**
     * 修改Floor
     *
     * @param floor
     * @return
     */
    @Override
    public int updateFloor(Floor floor) {
        if (floor.getFloorId() == null) {
            throw new ServiceException("楼层编号不能为空");
        }
        floor.setUpdateTime(new Date());
        return floorDao.updateByFloorId(floor);
    }

    /**
     * 删除Floor
     *
     * @param floorId
     * @return
     */
    @Override
    public int deleteFloorById(Integer floorId) {
        if (floorId == null) {
            throw new ServiceException("楼层编号不能为空");
        }
        Floor floor = new Floor();
        floor.setFloorId(floorId);
        floor.setDelStatus(1);
        return floorDao.updateByPrimaryKeySelective(floor);
    }

    @Override
    public List<Floor> listFloor(Integer position, Integer status, Integer shopId, Page page) {
        AssertUtils.notNull(position, "楼层位置不能为空");
        List<Floor> floors = floorDao.listFloor(position, status, page);
        List<Integer> floorIds = floors.stream().map(Floor::getFloorId).collect(Collectors.toList());
        List<FloorItem> floorItems = floorItemDao.listFloorItems(floorIds);
        Map<Integer, List<FloorItem>> floorItemGroup = floorItems.stream().collect(Collectors.groupingBy(FloorItem::getFloorId));
        for (Floor floor : floors) {
            floor.setFloorItemList(floorItemGroup.get(floor.getFloorId()));
        }
        //筛选出是商品的
        //筛选
        List<Integer> goodsIds = floorItems.stream().filter(floorItem -> Objects.equals(floorItem.getType(), FloorItemConst.TYPE_GOODS))
                .map(FloorItem::getItemObject).collect(Collectors.toList());
        //查询
        List<XcxGoods> goodsList = xcxGoodsDao.listByIds(goodsIds, shopId);
        //赋值
        for(FloorItem floorItem: floorItems) {
            goodsList.forEach(goods -> {
                if (Objects.equals(goods.getGoodsId(), floorItem.getItemObject())) {
                    floorItem.setXcxGoods(goods);
                }
            });
        }
        return floors;
    }
}
